import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutterlib/common/provider/themes_provider.dart';
import 'package:flutterlib/common/style/theme_group.dart';
import 'constants.dart';

NavigatorState get navigatorState => Instances.navigatorKey.currentState;

BuildContext get currentContext => navigatorState.context;

ThemeData get currentTheme => Theme.of(currentContext);
ThemeGroup get currentThemeGroup =>
    currentContext.read<ThemesProvider>().currentThemeGroup;
Brightness get currentBrightness => currentTheme.brightness;

Color get currentThemeColor => currentTheme.accentColor;

bool get currentIsDark => currentBrightness == Brightness.dark;

T lessThanOne<T extends num>(T value) =>
    math.min((value is int ? 1 : 1.0) as T, value);

T lessThanZero<T extends num>(T value) =>
    math.min((value is int ? 0 : 0.0) as T, value);

T moreThanOne<T extends num>(T value) =>
    math.max((value is int ? 1 : 1.0) as T, value);

T moreThanZero<T extends num>(T value) =>
    math.max((value is int ? 0 : 0.0) as T, value);

T betweenZeroAndOne<T extends num>(T value) => moreThanZero(lessThanOne(value));

DateTime get currentTime => DateTime.now();

int get currentTimeStamp => currentTime.millisecondsSinceEpoch;

class Instances {
  const Instances._();

  static final EventBus eventBus = EventBus();
  static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();
  static AppLifecycleState appLifeCycleState = AppLifecycleState.resumed;
  static ConnectivityResult connectivityResult;
  static final GlobalKey<ScaffoldState> mainPageScaffoldKey =
  GlobalKey<ScaffoldState>();
}
